#pragma once

const int COLOR_TABLE_SIZE = 65536;

// The color key structure. It stores
// the key position and the color.
struct SColorKey
{
public:

	SColorKey() : m_clr(0), m_pos(0.0f) {}
	SColorKey(COLORREF cl, float p) : m_clr(cl), m_pos(p) {}

	bool operator <(const SColorKey& pt)
	{
		return (m_pos < pt.m_pos);
	}

public:

	float    m_pos;
	COLORREF m_clr;
};


//The color interpolator class. It takes the color keys
//as the inputs and build color table based on it. Then
//it can give the color by it's float key:
//                                 0.0f <= key <= 1.0f.
class CColorInterpolator
{
public:
	CColorInterpolator();
	CColorInterpolator(COLORREF min, COLORREF max);
	virtual ~CColorInterpolator();

	//Keys access operations. This functions
	//don't update the color table.
	int GetNumKeys() const;
	SColorKey* GetKey(int i);
	const SColorKey* GetKey(int i) const;
	void RemoveKey(int i);
	void InsertKey(const SColorKey& pt);
	void SortKeys();//Call it if you've changed the
					//position of some point
					//Automatically updates the color table

	//val = [0.0f; 1.0f]
	COLORREF GetColor(double val) const;

private:

	void UpdateColors();

private:

	typedef std::vector<SColorKey> ColorKeys;

private:

	ColorKeys m_keys;
	COLORREF  m_colors[COLOR_TABLE_SIZE];
};